#include "ui/MainWindow.h"
#include "ui_MainWindow.h"
#include "core/Constans.h"

#include <QMenu>

MainWindow::MainWindow(BlackboardManager *blackboardManager) :
    QMainWindow(NULL), ui(new Ui::MainWindow), blackboardManager(blackboardManager) {
    ui->setupUi(this);

    paintWidget = new PaintWidget(this);
    setCentralWidget(paintWidget);

    lockAction = new QAction(QString("lock"), this);
    unlockAction = new QAction(QString("unlock"), this);
    lockLabel = new QLabel(QString("Lock: none"), this);

    ui->toolBar->addAction(lockAction);
    ui->toolBar->addAction(unlockAction);
    ui->toolBar->addSeparator();
    ui->toolBar->addWidget(lockLabel);

    connect(blackboardManager, SIGNAL(sizeChanged(int,int)), this, SLOT(sizeChange(int,int)));
    connect(paintWidget, SIGNAL(pointDrawed(QPoint)), blackboardManager, SLOT(pointDrawed(QPoint)));
    connect(blackboardManager, SIGNAL(pathsReceived(const QList<DrawPath*>*, QColor)), this, SLOT(drawPaths(const QList<DrawPath*>*, QColor)));
    connect(blackboardManager, SIGNAL(imageReceived(QImage)), this, SLOT(imagePaint(QImage)));
    connect(lockAction, SIGNAL(triggered()), this, SLOT(lockRequested()));
    connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockRequested()));
    connect(blackboardManager, SIGNAL(lockPaintChanged(bool)), this, SLOT(paintLockChanged(bool)));
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::sizeChange(int width, int height) {
    qDebug() << "Changing size " << width << "x" << height;
    resize(width, height + ui->toolBar->height());
}

void MainWindow::drawPaths(const QList<DrawPath *> * paths, QColor color) {
    paintWidget->drawPaths(paths, color);
}

void MainWindow::imagePaint(const QImage & image) {
    qDebug() << "Paint image " << image.width() << "x" << image.height();
    paintWidget->imagePaint(image);
}

void MainWindow::lockRequested() {
    blackboardManager->requestLock();
}

void MainWindow::unlockRequested() {
    blackboardManager->requestUnlock();
}

void MainWindow::paintLockChanged(bool value) {
    paintWidget->setLocked(value);
}

void MainWindow::show() {
    startupDialog = new StartupDialog(this);
    int returnCode = startupDialog->exec();

    QWidget::show();

    if (returnCode == QDialog::Rejected) {
        QMetaObject::invokeMethod(this, "close", Qt::QueuedConnection);
    } else {
        QColor penColor = startupDialog->getPenColor();
        paintWidget->setPaintColor(penColor);
        qDebug() << "Selected color " << penColor.name();

        if (startupDialog->isNewNetwork()) {
            NewNetworkDialog *nnDialog = startupDialog->getNewNetworkDialog();
            int width = nnDialog->getWidthParam();
            int height = nnDialog->getHeightParam();
            resize(width, height);

            blackboardManager->initNew(startupDialog->getListenAddress(), startupDialog->getListenPort(), penColor, width, height);
        }
        else if (startupDialog->isJoinSpecific()) {
            JoinSpecificDialog *jsDialog = startupDialog->getJoinSpecificDialog();

            blackboardManager->joinSpecific(startupDialog->getListenAddress(), startupDialog->getListenPort(),
                                            penColor, jsDialog->getJoinAddress(), jsDialog->getJoinPort());

        }
        else {
            //TODO
        }
        setWindowTitle(QString("Virtual blackboard - %1").arg(startupDialog->getListenPort()));
    }

    if (DEBUG) {
        nodeListDialog = new NodeListDialog(this, blackboardManager->getNodeListModel());
        nodeListDialog->show();
    }
}
